home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / CmplxQR.cc < prev    next >
C/C++ Source or Header  |  1997-07-10  |  3KB  |  154 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "CmplxQR.h"
  32. #include "f77-fcn.h"
  33. #include "lo-error.h"
  34. #include "mx-inlines.cc"
  35.  
  36. extern "C"
  37. {
  38.   int F77_FCN (zgeqrf, ZGEQRF) (const int&, const int&, Complex*,
  39.                 const int&, Complex*, Complex*,
  40.                 const int&, int&); 
  41.  
  42.   int F77_FCN (zungqr, ZUNGQR) (const int&, const int&, const int&,
  43.                 Complex*, const int&, Complex*,
  44.                 Complex*, const int&, int&);
  45. }
  46.  
  47. ComplexQR::ComplexQR (const ComplexMatrix& a, QR::type qr_type)
  48.   : q (), r ()
  49. {
  50.   init (a, qr_type);
  51. }
  52.  
  53. void
  54. ComplexQR::init (const ComplexMatrix& a, QR::type qr_type)
  55. {
  56.   int m = a.rows ();
  57.   int n = a.cols ();
  58.  
  59.   if (m == 0 || n == 0)
  60.     {
  61.       (*current_liboctave_error_handler)
  62.     ("ComplexQR must have non-empty matrix");
  63.       return;
  64.     }
  65.  
  66.   int min_mn = m < n ? m : n;
  67.  
  68.   Array<Complex> tau (min_mn);
  69.   Complex *ptau = tau.fortran_vec ();
  70.  
  71.   int lwork = 32*n;
  72.   Array<Complex> work (lwork);
  73.   Complex *pwork = work.fortran_vec ();
  74.  
  75.   int info = 0;
  76.  
  77.   ComplexMatrix A_fact;
  78.   if (m > n)
  79.     {
  80.       A_fact.resize (m, m);
  81.       A_fact.insert (a, 0, 0);
  82.     }
  83.   else
  84.     A_fact = a;
  85.  
  86.   Complex *tmp_data = A_fact.fortran_vec ();
  87.  
  88.   F77_XFCN (zgeqrf, ZGEQRF, (m, n, tmp_data, m, ptau, pwork, lwork, info));
  89.  
  90.   if (f77_exception_encountered)
  91.     (*current_liboctave_error_handler) ("unrecoverable error in zgeqrf");
  92.   else
  93.     {
  94.       if (qr_type == QR::raw)
  95.     {
  96.       for (int j = 0; j < min_mn; j++)
  97.         {
  98.           int limit = j < min_mn - 1 ? j : min_mn - 1;
  99.           for (int i = limit + 1; i < m; i++)
  100.         A_fact.elem (i, j) *= tau.elem (j);
  101.         }
  102.  
  103.       r = A_fact;
  104.  
  105.       if (m > n)
  106.         r.resize (m, n);
  107.     }
  108.       else
  109.     {
  110.       volatile int n2;
  111.  
  112.       if (qr_type == QR::economy && m > n)
  113.         {
  114.           n2 = n;
  115.           r.resize (n, n, 0.0);
  116.         }
  117.       else
  118.         {
  119.           n2 = m;
  120.           r.resize (m, n, 0.0);
  121.         }
  122.  
  123.       for (int j = 0; j < n; j++)
  124.         {
  125.           int limit = j < min_mn-1 ? j : min_mn-1;
  126.           for (int i = 0; i <= limit; i++)
  127.         r.elem (i, j) = A_fact.elem (i, j);
  128.         }
  129.  
  130.       lwork = 32*m;
  131.       work.resize (lwork);
  132.       Complex *pwork = work.fortran_vec ();
  133.  
  134.       F77_XFCN (zungqr, ZUNGQR, (m, m, min_mn, tmp_data, m, ptau,
  135.                      pwork, lwork, info));
  136.  
  137.       if (f77_exception_encountered)
  138.         (*current_liboctave_error_handler)
  139.           ("unrecoverable error in zungqr");
  140.       else
  141.         {
  142.           q = A_fact;
  143.           q.resize (m, n2);
  144.         }
  145.     }
  146.     }
  147. }
  148.  
  149. /*
  150. ;;; Local Variables: ***
  151. ;;; mode: C++ ***
  152. ;;; End: ***
  153. */
  154.